//
//  IncomeExpenseDetailViewController.m
//  Wallets Manager
//
//  Created by Tung Nguyen on 10/22/12.
//  Copyright (c) 2012 BKHN. All rights reserved.
//

#import "IncomeExpenseDetailViewController.h"

@interface IncomeExpenseDetailViewController ()

@end

@implementation IncomeExpenseDetailViewController
@synthesize pageViewController;
@synthesize dateHeaderLabel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //------------------Init firt page day == today ---------------//
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDate *date = [NSDate date];
    NSDateComponents *comps = [cal components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
                                     fromDate:date];
    [comps setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    today = [cal dateFromComponents:comps];
    
    //-------------------Set date header to standard-----------------------------//
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"MM/dd/yyyy"];
    dateHeaderLabel.text = [dateFormatter stringFromDate:today];
    //self.view.gestureRecognizers = self.pageViewController.gestureRecognizers;
    
    //-------------------Init Segment Control---------------------//
    
    SVSegmentedControl *grayRC = [[SVSegmentedControl alloc] initWithSectionTitles:[NSArray arrayWithObjects:@"Time Mode", @"Daily",@"Category ", nil]];
    [grayRC addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
    
	grayRC.font = [UIFont boldSystemFontOfSize:12];
	grayRC.titleEdgeInsets = UIEdgeInsetsMake(0, 14, 0, 14);
	grayRC.height = 30;
	
	grayRC.thumb.tintColor = [UIColor colorWithRed:0 green:0.5 blue:0.1 alpha:1];
	
	[self.view addSubview:grayRC];
	
	grayRC.center = CGPointMake(160,430);
    
    //-------------------Init Page View Controller-----------//
    self.pageViewController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    self.pageViewController.delegate = self;
    self.pageViewController.dataSource = self;
    self.pageViewController.view.frame = CGRectMake(0, 40, 320, 371);
    
    InExContentViewController *inexContentView = [[InExContentViewController alloc]init];
    inexContentView.currentDate = today;
    [self.pageViewController setViewControllers:[NSArray arrayWithObject:inexContentView] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    [self addChildViewController:pageViewController];
    [self.view addSubview:pageViewController.view];
    
    [self.pageViewController didMoveToParentViewController:self];
    
    
    //---------------------------NSNotification Center----------------------//
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertViewIncomePressed :) name:@"alertViewIncomePressed" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertViewExpensePressed :) name:@"alertViewExpensePressed" object:nil];
    
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(planCompleted : ) name:@"planCompleted" object:nil];

}

-(void)viewWillDisappear:(BOOL)animated {
    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"alertViewIncomePressed" object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"alertViewExpensePressed" object:nil];
}

- (void)viewDidUnload
{
    [self setDateHeaderLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)segmentedControlChangedValue:(SVSegmentedControl *)segmentedControl {
    NSLog(@"segmentedControl %i did select index %i (via UIControl method)", segmentedControl.tag, segmentedControl.selectedIndex);
}

#pragma mark ------------IBAction---------------
- (IBAction)backButtonPressed:(id)sender {
    //self.hidesBottomBarWhenPushed = NO;
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (IBAction)addButtonPressed:(id)sender {
    alertView = [[CustomAlertView alloc]initWithNibName:@"CustomAlertView" bundle:nil];
    
    [self.view addSubview:alertView.view];
    NSLog(@"%@", [self.view subviews]);
}

#pragma mark - UIPageViewControllerDataSource Methods

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
      viewControllerBeforeViewController:(UIViewController *)viewController
{

    InExContentViewController *inExContentViewController = [[InExContentViewController alloc] init];
    today = [today dateByAddingTimeInterval:-60*60*24];

    inExContentViewController.currentDate =  today;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"MM/dd/yyyy"];
    dateHeaderLabel.text = [dateFormatter stringFromDate:inExContentViewController.currentDate];
    return inExContentViewController;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
       viewControllerAfterViewController:(UIViewController *)viewController
{
    InExContentViewController *inExContentViewController = [[InExContentViewController alloc] init];
    today = [today dateByAddingTimeInterval:60*60*24];

    
    inExContentViewController.currentDate =  today;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"MM/dd/yyyy"];
    dateHeaderLabel.text = [dateFormatter stringFromDate:inExContentViewController.currentDate];
    return inExContentViewController;
}

#pragma mark - UIPageViewControllerDelegate Methods

- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController
                   spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    if(UIInterfaceOrientationIsPortrait(orientation))
    {
        //Set the array with only 1 view controller
        UIViewController *currentViewController = [self.pageViewController.viewControllers objectAtIndex:0];
        NSArray *viewControllers = [NSArray arrayWithObject:currentViewController];
        [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
        
        //Important- Set the doubleSided property to NO.
        self.pageViewController.doubleSided = NO;
        //Return the spine location
        return UIPageViewControllerSpineLocationMax;
    }
    else
    {
//        NSArray *viewControllers = nil;
//        ContentViewController *currentViewController = [self.pageViewController.viewControllers objectAtIndex:0];
//        
//        NSUInteger currentIndex = [self.modelArray indexOfObject:[(ContentViewController *)currentViewController labelContents]];
//        if(currentIndex == 0 || currentIndex %2 == 0)
//        {
//            UIViewController *nextViewController = [self pageViewController:self.pageViewController viewControllerAfterViewController:currentViewController];
//            viewControllers = [NSArray arrayWithObjects:currentViewController, nextViewController, nil];
//        }
//        else
//        {
//            UIViewController *previousViewController = [self pageViewController:self.pageViewController viewControllerBeforeViewController:currentViewController];
//            viewControllers = [NSArray arrayWithObjects:previousViewController, currentViewController, nil];
//        }
//        //Now, set the viewControllers property of UIPageViewController
//        [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
        
        return UIPageViewControllerSpineLocationMid;
    }
}

#pragma mark - Notification Center Delegate
- (void) alertViewIncomePressed : (NSNotification *) notification {
    AddIncomeViewController *addIncomeViewController = [[AddIncomeViewController alloc]init];
    addIncomeViewController.kindOfView = @"Add";
    [self.navigationController pushViewController:addIncomeViewController animated:YES];
}

- (void) alertViewExpensePressed : (NSNotification *) notification {
    AddExpenseViewController *addExpenseViewController = [[AddExpenseViewController alloc]init];
    addExpenseViewController.kindOfView = @"Add";
    [self.navigationController pushViewController:addExpenseViewController animated:YES];
}


-(void) planCompleted : (NSNotification*) notification {
    [AJNotificationView showNoticeInView:self.view
                                    type:AJNotificationTypeBlue
                                   title:[NSString stringWithFormat:@"Congratulation ! \n %@ has completed",(NSString*)[notification object]]
                         linedBackground:AJLinedBackgroundTypeDisabled
                               hideAfter:3.0f];
}


@end
